package cn.graydove.bot.listener;

import cn.graydove.bot.adapter.GroupMessageEvent;
import cn.graydove.bot.adapter.RobotClient;
import cn.graydove.bot.annotation.Listener;
import cn.graydove.bot.data.GroupServiceCacheData;
import cn.graydove.bot.core.Assert;
import cn.graydove.bot.model.entity.User;
import lombok.extern.slf4j.Slf4j;
import net.mamoe.mirai.message.data.At;
import org.springframework.stereotype.Component;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
@Slf4j
public class AdminListener {
    private Assert asserts;
    private GroupServiceCacheData data;

    public AdminListener(Assert asserts, GroupServiceCacheData groupServiceCacheData) {
        this.asserts = asserts;
        this.data = groupServiceCacheData;
    }

    @Listener("^赋予权限.*")
    public void grant(GroupMessageEvent event, RobotClient client) {
        asserts.groupHostReceive(event, (group) -> {
            try {
                At at = client.getAt();
                Long qq = null;
                if (at != null) {
                    qq = at.getTarget();
                }
                if (qq == null) {
                    qq = client.getNumber();
                }
                for (var a:group.getAdministrator()) {
                    if (qq.equals(a.getQqNumber())) {
                        client.sendGroupMsg(event.getGroupId(), "该用户已是管理员");
                        return;
                    }
                }
                group = data.ensureUserAndGetGroup(group, client, qq);
                User user = data.findUser(group, qq);
                if (user == null) {
                    throw new RuntimeException();
                }
                data.grant(group, qq);
                client.sendGroupMsg(event.getGroupId(), "已将 " + user.getName() + " 设为管理员");
            } catch (Exception e) {
                client.sendGroupMsg(event.getGroupId(), "赋予权限失败");
            }
        });
    }


    @Listener("^回收权限.*")
    public void delete_grant(GroupMessageEvent event, RobotClient client) {
        asserts.groupHostReceive(event, (group) -> {
            try {
                At at = client.getAt();
                Long qq = null;
                if (at != null) {
                    qq = at.getTarget();
                }
                if (qq == null) {
                    qq = client.getNumber();
                }
                group = data.ensureUserAndGetGroup(group, client, qq);
                User user = data.findUser(group, qq);
                if (user == null) {
                    throw new RuntimeException();
                }
                data.revoke(group, qq);
                client.sendGroupMsg(event.getGroupId(), "已回收 " + user.getName() + " 的管理员权限");
            } catch (Exception e) {
                client.sendGroupMsg(event.getGroupId(), "回收权限失败");
            }
        });
    }


    @Listener("^查看管理$")
    public void show_authorization(GroupMessageEvent event, RobotClient client) {
        asserts.groupReceive(event, (group) -> {
            StringBuilder sb = new StringBuilder();
            Long host = group.getHost().getQqNumber();
            group = data.ensureUserAndGetGroup(group, client, host);
            User user = data.findUser(group, host);
            if (user == null) {
                user = new User(host, "--该用户不存在--");
            }
            sb.append("超级管理员：")
                    .append("\n\t")
                    .append(user.getName())
                    .append(" ")
                    .append(user.getQqNumber());
            sb.append("\n管理员：");
            for (var administrator: group.getAdministrator()) {
                long qq = administrator.getQqNumber();
                group = data.ensureUserAndGetGroup(group, client, qq);
                user = data.findUser(group, qq);
                if (user == null) {
                    user = new User(qq, "--该用户不存在--");
                }
                sb.append("\n\t")
                        .append(user.getName())
                        .append(" ")
                        .append(user.getQqNumber());
            }
            client.sendGroupMsg(event.getGroupId(), sb.toString());
        });
    }

    @Listener("^启动Bot$")
    public void start(GroupMessageEvent event, RobotClient client) {
        asserts.builder(event)
                .isGroup()
                .isNotAlive()
                .isAdmin()
                .accept((group)->{
            data.start(event.getGroupId());
            client.sendGroupMsg(event.getGroupId(), "Bot已启动");
        });
    }

    @Listener("^关闭Bot$")
    public void stop(GroupMessageEvent event, RobotClient client) {
        asserts.groupAdminReceive(event, (group)->{
            data.stop(event.getGroupId());
            client.sendGroupMsg(event.getGroupId(), "Bot已关闭");
        });
    }

    @Listener({"^查看指令$", "^指令详情$", "^帮助$"})
    public void help(GroupMessageEvent event, RobotClient client) {
        asserts.groupReceive(event, (group)-> client.sendGroupMsg(event.getGroupId(),
            "Bot v1.1.6 [mirai版]\n" +
                    "# 括号内容为描述，不需要输入！\n" +
                    "# 输入指令时不要带<>！\n" +
                    "# 参数使用空格隔开！\n" +
                    "\n" +
                    "查看指令\n" +
                    "赋予权限 <QQ号>(需超级管理员权限)\n" +
                    "回收权限 <QQ号>(需超级管理员权限)\n" +
                    "赋予权限 @被授予权限的成员(需超级管理员权限)\n" +
                    "回收权限 @被撤回权限的成员(需超级管理员权限)\n" +
                    "查看管理\n" +
                    "启动Bot(需管理员权限)\n" +
                    "关闭Bot(需管理员权限)\n" +
                    "报刀 <伤害数值>\n" +
                    "代报刀 <伤害数值> @被代刀成员\n" +
                    "申请出刀\n" +
                    "完成 <伤害数值>\n" +
                    "补偿刀 <伤害数值>\n" +
                    "代刀 <伤害数值> @被代刀成员\n" +
                    "代刀补偿刀 <伤害数值> @被代刀成员\n" +
                    "查看\n" +
                    "挂树\n" +
                    "查树\n" +
                    "强行下树 <伤害数值>\n" +
                    "修正 <周目数> <第几Boss> <剩余生命>(需管理员权限)\n" +
                    "出刀统计\n" +
                    "未出刀统计\n" +
                    "模拟 <周目数> <第几Boss> <伤害>\n" +
                    "模拟统计\n" +
                    "删除模拟记录\n" +
                    "清空模拟记录(需管理员权限)\n" +
                    "开启准备统计(需管理员权限)\n" +
                    "结束准备统计(需管理员权限)\n" +
                    "查看准备统计\n" +
                    "准备\n" +
                    "取消准备\n" +
                    "导出模拟Excel\n" +
                    "导出出刀Excel\n" +
                    "设置血线(需管理员权限)\n" +
                    "涩图 <涩图id,若不指定则随机涩图>（暂时关闭）\n" +
                    "上传涩图[图片]（暂时关闭）\n" +
                "\n双矿剩余在线计算：https://gzlj.u0z1.com/\n" +
                "指令详细文档请访问https://blog.graydove.cn/archives/pcrbot查看"));
    }
}
